// 校验手机号格式是否正确（只检测大陆手机号）
export const checkMobileValid = (number) => {
    const reg = /^(\+?0?86\-?)?1[3456789]\d{9}$/;
    return reg.test(number);
}

// 校验邮箱格式是否正确
export const checkEmailValid = (email) => {
    const reg = /^([a-zA-Z]|[0-9])(\w|\-)+@([a-zA-Z0-9]|\-)+\.([a-zA-Z]{2,4})$/;
    return reg.test(email);
};

// 千/万 转换 1000 >> 1k  10000 >> 1w
export const numConvert = function (num) {
    if (num >= 10000) {
        num = Math.round(num / 1000) / 10 + "w";
    } else if (num >= 1000) {
        num = Math.round(num / 100) / 10 + "k";
    }
    return num;
};

// 设置cookie
export const setCookie = (name, value, expires, path, domain, secure) => {
    var today = new Date();
    today.setTime(today.getTime());
    if (expires) {
        expires = expires * 1000 * 60 * 60 * 24;
    }
    var expires_date = new Date(today.getTime() + (expires));
    document.cookie = name + '=' + escape(value) +
        ((expires) ? ';expires=' + expires_date.toGMTString() : '') + //expires.toGMTString() 
        ((path) ? ';path=' + path : '') +
        ((domain) ? ';domain=' + domain : '') +
        ((secure) ? ';secure' : '');
}
// 获取cookie
export const getCookie = (name) => {
    var start = document.cookie.indexOf(name + "=");
    var len = start + name.length + 1;
    if ((!start) && (name != document.cookie.substring(0, name.length))) {
        return null;
    }
    if (start == -1) return null;
    var end = document.cookie.indexOf(';', len);
    if (end == -1) end = document.cookie.length;
    return unescape(document.cookie.substring(len, end));
}